package com.coocaa.ops.admin.v1.dmp.tag.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.admin.v1.dmp.tag.constant.DmpTagTypeConstants;
import com.coocaa.ops.admin.v1.dmp.tag.model.DmpPolicyQueryModel;
import com.coocaa.ops.admin.v1.dmp.tag.model.DmpTagInfo;
import com.coocaa.ops.admin.v1.dmp.tag.model.DmpTagQueryModel;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.common.tool.exception.ExceptionUtil;
import com.coocaa.ops.common.tool.json.ParseJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangminghong
 * @date 2020-06-18
 */
@Slf4j
public class DmpTagDataConverter {

    /**
     * 获取DMP人群策略查询条件(无分页)
     *
     * @return
     */
    public static Map<String, String> getQueryMapForCrowdPolicy() {
        Map params = new HashMap(3);
        handleDmpPolicyQueryMap(params);
        return params;
    }
    /**
     * 获取DMP人群策略查询条件(分页)
     *
     * @return
     */
    public static Map<String, String> getPageQueryMapForCrowdPolicy(DmpPolicyQueryModel queryModel) {
        Map params = InstanceUtil.newHashMap(4);
        params.put("pageNum", queryModel.getPage().toString());
        params.put("pageSize", queryModel.getRows().toString());
        //主页id
        params.put("biId", "2");
        if (StringUtils.isNotBlank(queryModel.getPolicyName())){
            params.put("s", queryModel.getPolicyName());
        }
        if (queryModel.getPolicyId() != null) {
            params.put("policyId", queryModel.getPolicyId());
        }
        return params;
    }

    public static Map<String, String> getQueryMapForCrowd(DmpTagQueryModel queryModel) {
        Map<String, String> params = InstanceUtil.newHashMap(3);
        handleDmpCrowdQueryMap(params);
        params.put("policyId", String.valueOf(queryModel.getId()));
        return params;
    }

    /**
     *
     * @param result
     * @param type {@link com.coocaa.ops.admin.v1.dmp.tag.constant.DmpTagTypeConstants}
     * @return
     */
    public static List<DmpTagInfo> converterTagList(String result, String type) {
        List<DmpTagInfo> dmpTagInfoList = InstanceUtil.newArrayList();
        int index = 0;
        JSONArray jsonArray = null;
        try {
            jsonArray = ParseJsonUtil.parseJSONArray(ParseJsonUtil.parseJSONObject(result).getJSONObject("data"), "list");
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTraceAsString(e));
            return dmpTagInfoList;
        }
        int size = jsonArray.size();
        for (; index < size; index++) {
            JSONObject jsonObj = jsonArray.getJSONObject(index);
            if (DmpTagTypeConstants.CROWD_POLICY.equals(type)) {
                dmpTagInfoList.add(getEachTagForCrowdPolicy(jsonObj));
            } else if (DmpTagTypeConstants.CROWD.equals(type)) {
                dmpTagInfoList.add(getEachTagForCrowd(jsonObj));
            }
        }
        return dmpTagInfoList;
    }

    public static PageResultModel<DmpTagInfo> converterPageResultForCrowdPolicy(String result) {
        try {
            Integer total = ParseJsonUtil.parseIntValue(ParseJsonUtil.parseJSONObject(result).getJSONObject("data"), "total");
            List<DmpTagInfo> tagInfoList = converterTagList(result, DmpTagTypeConstants.CROWD_POLICY);
            return PageResultModel.getPageResultModel(total, tagInfoList);
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTraceAsString(e));
            List<DmpTagInfo> tagInfoList = InstanceUtil.newArrayList();
            return PageResultModel.getPageResultModel(Integer.valueOf(0), tagInfoList);
        }



    }

    private static DmpTagInfo getEachTagForCrowdPolicy(JSONObject crowdPolicyJson) {
        Integer policyId = crowdPolicyJson.getInteger("policyId");
        String policyName = crowdPolicyJson.getString("policyName");
        DmpTagInfo tagInfo = new DmpTagInfo(policyId, policyName);
        return tagInfo;
    }

    private static DmpTagInfo getEachTagForCrowd(JSONObject crowdPolicyJson) {
        Integer crowdId = crowdPolicyJson.getInteger("crowdId");
        String crowdName = crowdPolicyJson.getString("crowdName");
        DmpTagInfo tagInfo = new DmpTagInfo(crowdId, crowdName);
        return tagInfo;
    }



    /**
     * 处理策略分页设置
     *
     * @param params
     */
    private static void handleDmpPolicyQueryMap(Map<String, String> params) {
        params.put("pageNum", "1");
        params.put("pageSize", "100");
        //主页id
        params.put("biId", "2");
    }

    /**
     * 处理人群分页设置
     *
     * @param params
     */
    private static void handleDmpCrowdQueryMap(Map<String, String> params) {

        params.put("pageNum", "1");
        params.put("pageSize", "100");
    }
}
